<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>捕获 ESP32 数据</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
    }
    pre {
      background-color: #f4f4f4;
      padding: 10px;
      overflow-x: auto;
    }
  </style>
</head>
<body>
<h1>捕获 ESP32 数据</h1>

<h2>ESP32 发送的数据</h2>
<pre id="requestData"></pre>

<h2>返回的数据</h2>
<pre id="responseData"></pre>

<script>
  const POLLING_INTERVAL = 5000; // 轮询间隔，单位：毫秒

  function pollData() {
    fetch('http://localhost:8080/get-esp32-data', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    })
            .then(response => {
              if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
              }
              return response.json();
            })
            .then(data => {
              document.getElementById('requestData').textContent = data.request;
              document.getElementById('responseData').textContent = data.response;
            })
            .catch(error => {
              console.error('轮询出错:', error);
            })
            .finally(() => {
              // 定时再次轮询
              setTimeout(pollData, POLLING_INTERVAL);
            });
  }

  // 启动轮询
  pollData();
</script>
</body>
</html>